package com.framework.loippi.service.impl;

import com.framework.loippi.service.RedisService;
import com.framework.loippi.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

/**
 * redisservice
 * Created by longbh on 16/9/6.
 */
@Service("redisServiceImpl")
public class RedisServiceImpl implements RedisService {

    ///--------redis-------------
    private Boolean redisEnabled = true;

    @Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;
    @Value("${redis.db}")
    private Integer db;
    @Value("${redis.maxIdle}")
    private Integer maxIdle;
    @Value("${redis.maxTotal}")
    private Integer maxTotal;
    @Value("${redis.maxWaitMillis}")
    private Long maxWaitMillis;
    @Value("${redis.timeout}")
    private Integer timeout;
    @Value("${redis.pass}")
    private String password;
    private RedisUtils redisUtils;

    @PostConstruct
    public void init() {
        JedisPoolConfig config = new JedisPoolConfig();
        // 连接池配置
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        config.setMaxWaitMillis(maxWaitMillis);
        redisUtils = new RedisUtils(config, host, port, timeout, password,db);
    }

    @Override
    public void delete(String key) {
        if (key == null) return;
        redisUtils.del(key);
    }

    /**
     * 添加缓存
     *
     * @param key
     * @param object
     */
    @Override
    public void save(String key, Object object) {
        if (key == null) return;
        redisUtils.set(key, object);
    }
    /**
     * 添加缓存
     *
     * @param key
     * @param object
     */
    @Override
    public void save(String key, Object object,int seconds) {
    	if (key == null) return;
    	redisUtils.set(key, object, seconds);
    }

    @Override
    public void lpush(String key, Object... object) {
        if (null == key) {
            return;
        }
        redisUtils.lpush(key, object);
    }

    @Override
    public long incr(String key, long incr) {
        if (null == key) {
            return 0L;
        }
        return redisUtils.incr(key, incr);
    }

    @Override
    public long decr(String key, long decr) {
        if (null == key) {
            return 0L;
        }
        return redisUtils.decr(key, decr);
    }

    @Override
    public <T> T brpop(String key, int timeout, Class<T> clazz) {
        return redisUtils.brpop(key, timeout, clazz);
    }

    @Override
    public <T> T blpop(String key, int timeout, Class<T> clazz) {
        return redisUtils.blpop(key, timeout, clazz);
    }

    @Override
    public <T> T lpop(String key, Class<T> type) {
        return redisUtils.lpop(key, type);
    }

    @Override
    public <T> T rpop(String key, Class<T> type) {
        return redisUtils.rpop(key, type);
    }

    @Override
    public <T> T get(String key, Class<T> type) {
        return redisUtils.get(key, type);
    }


}
